﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

/// <summary>
/// 枚举操作
/// </summary>
/// <author>邓晨露</author>
namespace Common.Utility
{
    public static class EnumHelper
    {
        /// <summary>  
        /// 扩展方法：根据枚举值得到相应的枚举定义字符串  
        /// </summary>  
        /// <param name="value"></param>  
        /// <param name="enumType"></param>  
        /// <returns></returns>  
        public static String ToEnumString(this int value, Type enumType)
        {
            List<SelectItem> itemList = GetEnumStringFromEnumValue(enumType);
            return itemList.FirstOrDefault(p => p.Value == Convert.ToString(value))?.Text;
        }

        /// <summary>  
        /// 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合  
        /// </summary>  
        /// <param name="enumType"></param>  
        /// <returns></returns>  
        public static List<SelectItem> GetEnumStringFromEnumValue(Type enumType)
        {
            List<SelectItem> itemList = new List<SelectItem>();
            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            string strValue = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                    strText = field.Name;
                    itemList.Add(new SelectItem(strText, strValue));
                }
            }
            return itemList;
        }

        /// <summary>  
        /// 扩展方法：根据枚举值得到属性Description中的描述, 如果没有定义此属性则返回空串  
        /// </summary>  
        /// <param name="value"></param>  
        /// <param name="enumType"></param>  
        /// <returns></returns>  
        public static String ToEnumDescriptionString(this int value, Type enumType)
        {
            List<SelectItem> itemList = GetNVCFromEnumValue(enumType);
            return itemList.FirstOrDefault(p => p.Value == Convert.ToString(value))?.Text;
        }

        /// <summary>  
        /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合  
        /// </summary>  
        /// <param name="enumType"></param>  
        /// <returns></returns>  
        public static List<SelectItem> GetNVCFromEnumValue(Type enumType)
        {
            List<SelectItem> itemList = new List<SelectItem>();
            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            string strValue = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                        strText = aa.Description;
                    }
                    else
                    {
                        strText = "";
                    }
                    itemList.Add(new SelectItem(strText, strValue));
                }
            }
            return itemList;
        }

        /// <summary>  
        /// 扩展方法：根据枚举值得到属性Description中的描述, 如果没有定义此属性则返回空串  
        /// </summary>  
        /// <param name="value"></param>  
        /// <param name="enumType"></param>  
        /// <returns></returns>  
        public static String ToEnumDescriptionString(this string value, Type enumType)
        {
            List<SelectItem> itemList = GetNVCFromEnumString(enumType);
            return itemList.FirstOrDefault(p => p.Value == value)?.Text;
        }

        /// <summary>  
        /// 根据枚举类型得到其枚举定义字符串 与 枚举定义Description属性 的集合  
        /// </summary>  
        /// <param name="enumType"></param>  
        /// <returns></returns>  
        public static List<SelectItem> GetNVCFromEnumString(Type enumType)
        {
            List<SelectItem> itemList = new List<SelectItem>();
            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            string strValue = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    strValue = field.Name;
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                        strText = aa.Description;
                    }
                    else
                    {
                        strText = "";
                    }
                    itemList.Add(new SelectItem(strText, strValue));
                }
            }
            return itemList;
        }
    }

    public class SelectItem
    {

        public SelectItem(string text, string value)
        {
            Text = text;
            Value = value;
        }
        public string Text { set; get; }
        public string Value { set; get; }
    }
}
